<template>
  <div class="container">
    <div class="title">
       <span>添加机器</span> <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
      </div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="机器id" prop="mid">
              <el-input size="medium" v-model="form.mid" placeholder="请填写机器id"></el-input>
            </el-form-item>
            <el-form-item label="机器名称" prop="mname">
              <el-input size="medium" v-model="form.mname" placeholder="请填写机器名称"></el-input>
            </el-form-item>
            <el-form-item label="机器地址" prop="location">
              <el-input size="medium" v-model="form.location" placeholder="请填写机器地址"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                size="medium"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 8 }"
                placeholder="请输入备注"
                v-model="form.remark"
              >
              </el-input>
            </el-form-item>

            <el-form-item class="submit">
              <el-button type="primary"  @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import machine from '@/model/machine'

export default {
  data() {
    return {
      form: {
        mid: '',
        mname: '',
        location: '',
        remark: '',
      },
      loading: false,
    }
  },
  methods: {
    async submitForm(formName) {
      try {
        this.loading = true
        const res = await machine.createMachine(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
        }
      } catch (error) {
        this.loading = false
        this.$message.error('机器添加失败，请检测填写信息')
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('addClose')
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }
  .back {
    float: right;
    margin-right: 40px;
    cursor: pointer;
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
